package com.achobeta.recruitment.controller.wx;

import com.achobeta.recruitment.common.AppResult;
import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.model.WxUser;
import com.achobeta.recruitment.service.wx.WxUserService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.SessionUtils;
import com.achobeta.recruitment.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-27
 * Time: 22:25
 */

@Slf4j
@RestController
@RequestMapping("/wxuser")
@Api(tags = "微信用户相关接口")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    @Value("${recruit.resume.url}")
    private String resume;


    @Value("${redirect.fe}")
    private String redirectFE;


    @ApiOperation(value = "(授权)获取登录用户信息", notes = "无data")
    @RequestMapping(value = "/getSignUpUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public AppResult getSignUpUserInfo(HttpServletRequest request,
                                       HttpServletResponse response,
                                       @RequestParam(value = "url", required = false) String url)throws IOException {
        //获取code
        String code = request.getParameter("code");
        // code为null
        if(code == null) {
            log.warn(ResultCode.FAILED_UNAUTHORIZED.toString());
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        WxUser wxUser = wxUserService.getSignUpUserInfo(code);
        log.warn(SessionUtils.getWxUser(request) == null ? "没有会话！！！" : SessionUtils.getWxUser(request).toString());
        SessionUtils.setWxUser(request, wxUser);
        log.warn(SessionUtils.getWxUser(request) == null ? "没有会话！！！" : SessionUtils.getWxUser(request).toString());
        // 重定向
        log.warn(url);
        response.sendRedirect(redirectFE + (!StringUtils.hasLength(url) ? resume : url));
        return AppResult.success();
    }




    @GetMapping("/check")
    @ApiOperation(value = "用户登录检测", notes = "当前用户登录信息（如果登录了的话）")
    public AppResult checkLogin(HttpServletRequest request) {
        // 这个接口应该只被wx用户拦截器拦截，没被拦截器拦截则代表已登录
        return AppResult.success(SessionUtils.getWxUser(request));
    }

    @ApiOperation(value = "[仅测试阶段开放] 用户登录接口", notes = "无data")
    @PostMapping("/login")
    public AppResult login(HttpServletRequest request,
                           @NonNull @RequestParam("openid") @ApiParam("用户openid") String openid) {
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        WxUser wxUser = wxUserService.selectWxUserByOpenid(openid);
        CheckUtils.checkNULL(wxUser);
        SessionUtils.setWxUser(request, wxUser);
        return AppResult.success();
    }


}
